package com.example.cet4try.MyFragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.example.cet4try.R;
import com.example.cet4try.activity.ActivityBankedCloze;
import com.example.cet4try.activity.ActivityBlanksSpelling;
import com.example.cet4try.activity.ActivityChineseEnglishTest;
import com.example.cet4try.activity.ActivityEnglishChineseTest;
import com.example.cet4try.activity.ActivityWordList;
import com.example.cet4try.activity.ActivityWordSpelling;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;

public class MyFragment1 extends Fragment implements OnBannerListener {
    private Banner mBanner;
    private MyImageLoader mMyImageLoader;
    private ArrayList<Integer> imagePath;
    private ArrayList<String> imageTitle;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.page1,container,false);

        initData();
        initView(v);

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = null;
                switch (v.getId()){
                    case R.id.btn_cnen:
                        intent = new Intent(getContext(),ActivityChineseEnglishTest.class);
                        break;
                    case R.id.btn_encn:
                        intent = new Intent(getContext(), ActivityEnglishChineseTest.class);
                        break;
                    case R.id.btn_pin:
                        intent = new Intent(getContext(), ActivityWordSpelling.class);
                        break;
                    case R.id.btn_revise:
                        intent = new Intent(getActivity(), ActivityWordList.class);
                        intent.putExtra("type","all");
                        break;
                    case R.id.btn_cloze:
                        intent = new Intent(getActivity(), ActivityBankedCloze.class);
                        break;
                    case R.id.btn_pin2:
                        intent = new Intent(getActivity(), ActivityBlanksSpelling.class);
                    default:break;
                }
                if (intent != null)
                    startActivity(intent);
            }
        };

        SharedPreferences preferences = getActivity().getSharedPreferences("App_Shared_Data", 0);
        String username = preferences.getString("username","");
        boolean isYk = username.equals("yk");

        Button btn_cnen,btn_encn,btn_pin,btn_revise,btn_cloze,btn_pin2;

        btn_cnen = v.findViewById(R.id.btn_cnen);
        btn_cnen.setOnClickListener(listener);
        btn_encn = v.findViewById(R.id.btn_encn);
        btn_encn.setOnClickListener(listener);
        btn_pin = v.findViewById(R.id.btn_pin);
        btn_pin.setOnClickListener(listener);
        btn_revise = v.findViewById(R.id.btn_revise);
        btn_revise.setOnClickListener(listener);
        btn_cloze = v.findViewById(R.id.btn_cloze);
        btn_cloze .setOnClickListener(listener);
        btn_pin2 = v.findViewById(R.id.btn_pin2);
        btn_pin2.setOnClickListener(listener);

        if (isYk){
            btn_cnen.setVisibility(View.GONE);
            btn_encn.setVisibility(View.GONE);
            btn_pin.setVisibility(View.GONE);
            btn_cloze.setVisibility(View.GONE);
            btn_pin2.setVisibility(View.GONE);
        }

        return v;
    }

    private void initData() {
        imagePath = new ArrayList<>();
        imageTitle = new ArrayList<>();
        imagePath.add(R.mipmap.pic1);
        imagePath.add(R.mipmap.pic2);
        imagePath.add(R.mipmap.pic3);
        imagePath.add(R.mipmap.pic4);
        imagePath.add(R.mipmap.pic5);
        imageTitle.add("广告一");
        imageTitle.add("广告二");
        imageTitle.add("广告三");
        imageTitle.add("广告四");
        imageTitle.add("广告五");
    }
    private void initView(View v) {
        mMyImageLoader = new MyImageLoader();
        mBanner = v.findViewById(R.id.banner);
        //设置样式，里面有很多种样式可以自己都看看效果
        mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        //设置图片加载器
        mBanner.setImageLoader(mMyImageLoader);
        //设置轮播的动画效果,里面有很多种特效,可以都看看效果。
        //mBanner.setBannerAnimation(Transformer.ZoomOutSlide);
        mBanner.setBannerAnimation(Transformer.Default);
        //轮播图片的文字
        mBanner.setBannerTitles(imageTitle);
        //设置轮播间隔时间
        mBanner.setDelayTime(3000);
        //设置是否为自动轮播，默认是true
        mBanner.isAutoPlay(true);
        //设置指示器的位置，小点点，居中显示
        mBanner.setIndicatorGravity(BannerConfig.CENTER);
        //设置图片加载地址
        mBanner.setImages(imagePath)
                //轮播图的监听
                .setOnBannerListener(this)
                //开始调用的方法，启动轮播图。
                .start();

    }

    /**
     * 轮播图的监听
     *
     * @param position
     */
    @Override
    public void OnBannerClick(int position) {
        //Toast.makeText(getActivity(), "你点了第" + (position + 1) + "张轮播图", Toast.LENGTH_SHORT).show();
    }
    /**
     * 图片加载类
     */
    private class MyImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context.getApplicationContext())
                    .load(path)
                    .into(imageView);
        }
    }

}
